home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3853 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.6 KB

  1. Path: news.delphi.com!usenet
  2. From: Derek Harmon <stonelight@delphi.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: need convertion from char far* to char near *?
  5. Date: Tue, 30 Jan 96 12:52:37 -0500
  6. Organization: Delphi (info@delphi.com email, 800-695-4005 voice)
  7. Message-ID: <JlCqgr9.stonelight@delphi.com>
  8. References: <4ej91m$12ei@flood.weeg.uiowa.edu>
  9. NNTP-Posting-Host: bos1f.delphi.com
  10. X-To: Krzysztof Cwalina <kcwalina@blue.weeg.uiowa.edu>
  11.  
  12. ** Quoting a message from <kcwalina@blue.weeg.uiowa.edu> dated <29-Jan-1996>:
  13.  
  14. > I'm working on PC and I have a problem:
  15.  
  16. > I wan't to use atoi but it only works with near strings. Unfortunately, I
  17. > have a far char pointer and I don't know how to convert it into a near
  18. > one.
  19.  
  20.     A near pointer is good only within a 64K segment, it is 2-bytes long,
  21. hence, it can be in the range of 0 to 65,535.  Your PC knows where in
  22. memory this is, because it adds this offset to the 16-bit DS (Data Segment)
  23. register, allowing you to address any one 64K segment of memory in a 32-bit
  24. address space.  Sneaky, those folks at Intel, huh?
  25.  
  26.     In the PC's Compact, Large and Huge memory models, you can have up to 1
  27. MB of static data.  To accomplish this, all pointers are implicitly made far.
  28. What this means is the DS, which was previously the implied upper 16-bits of
  29. any near pointer's effective address, is no longer consulted.  Instead, a
  30. far pointer is 4-bytes long, with its own segment (first 16-bits) and offset
  31. (last 16-bits).  This means it can point anywhere it darn well pleases.
  32.  
  33.     I explain this to emphasize my two points in responding to your question.
  34.  
  35.     1)  It is "impossible" to behead a 4-byte far pointer into a 2-byte near
  36. pointer.  You would irrevocably lose information.  The reverse, to extend
  37. a 2-byte near pointer into a 4-byte far pointer, is trivial and can be type-
  38. cast normally.  Having said this, an exception is when the segment of the
  39. far pointer is equal to the Data Segment, and it is hence equivalent to a
  40. near pointer (speaking strictly of Turbo C++, the compiler appears to do
  41. this with statically declared far pointers, at least the first 64KB worth).
  42.  
  43.     2)  The people who invented the convoluted scheme of these Memory Models
  44. and write compiler libraries, already know this.  So, I suggest this pearl
  45. of programming wisdom:
  46.  
  47.     When you run into difficult memory constraints... compile using the next
  48. bigger memory model!  :)
  49.  
  50.     Most PC compilers I can think of, certainly from Microsoft or Borland,
  51. will have libraries in each memory model, employing whatever pointer is the
  52. norm.  I would suggest going into your compiler's Compiler or Linker Options
  53. and set to the Large Memory Model.
  54.  
  55. In your example,
  56.  
  57. (Using Small memory model: Near code and Near data, CS != DS)
  58.  
  59. (i.) char *number;  /* by default, this is a near pointer */
  60.      int value;
  61.      :
  62.      value = atoi(number);  /* this will work fine */
  63.  
  64. (ii.) char far *number;  /* overriding normal near pointer, making it far */
  65.       int value;
  66.       :
  67.       value = atoi(number);  /* CRASH!  atoi() expects near */
  68.       /* Note: Worked in TC++, again I suspect it sets far segment to DS */
  69.       /* when it can.  Let's just say this is a bad idea to try.  :) */
  70.  
  71. (iii.) char far *number;  /* overriding normal near pointer, making it far */
  72.        int value;
  73.        :
  74.        value = atoi((near)number);   /* CRASH! atoi() only gets offset */
  75.        /* Note: Again, this might work when Seg(number) == DS.  Bad idea. */
  76.  
  77. (Using Large memory model: Far code and Far data)
  78.  
  79. (iv.)  char *number;  /* by default, this is a far pointer */
  80.        int value;
  81.        :
  82.        value = atoi(number);  /* fine, as atoi() expects a far pointer */
  83.        /* atoi() also uses a far jump and return, so your code doesn't */
  84.        /* have to necessarily fit into one 64KB code segment */
  85.  
  86. (v.)  char near *number;  /* overriding default far pointer */
  87.       int value;
  88.       :
  89.       value = atoi(number);  /* atoi() expects a far pointer, but TC++ */
  90.       /* will implicitly extend number to make the segment DS. */
  91.  
  92. (vi.)  char near *number;  /* overriding default far pointer */
  93.        int value;
  94.        :
  95.        value = atoi((far)number);  /* this is the proper typecast & works */
  96.  
  97.     Try reading up on the linking options for your specific compiler so that
  98. you can link the larger memory model versions of the standard libraries
  99. into your program.  Borland's Turbo C++ includes C0x.OBJ (program start-up
  100. module), MATHx.LIB, and Cx.LIB (standard library) for each memory model x
  101. (where x is the first letter of the model, ie, 's' for small, 'h' for huge).
  102.  
  103.                                                                   - Stone
  104.  
  105. --
  106. ... recursive, adj.  see recursive.
  107.